package com.dqhl.community.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.fragment.HomePageFragment;
import com.dqhl.community.fragment.LifeFragment;
import com.dqhl.community.fragment.PersonalFragment;
import com.dqhl.community.fragment.PropertyFragment;
import com.dqhl.community.util.Constant;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private boolean quit = false; //设置退出标识
    private static final int HOME_PAGE = 0;
    private static final int PROPERTY = 1;
    private static final int LIFE = 2;
    private static final int PERSONAL = 3;
    private FragmentManager fm;
    private HomePageFragment homePageFragment; // 首页fragment
    private PropertyFragment propertyFragment; // 物业fragment
    private LifeFragment lifeFragment; // 生活fragment
    private PersonalFragment personalFragment; // 我的fragment

    private ImageView ivHomePage; // 首页按钮
    private ImageView ivProperty; // 物业按钮
    private ImageView ivLife; // 生活按钮
    private ImageView ivPersonal; // 我的按钮
    private String userId;
    private String authentication; //是否是认证用户

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        userId = spUserInfo.getString(Constant.USERID,"");
        if (!"".equals(userId) && userId.length() > 0){
            app.setUserId(userId);
        }
        authentication = spUserInfo.getString(Constant.AUTHENTICATION, "");
        if (!"".equals(authentication) && authentication.length() > 0){
            app.setAuthentication(authentication);
        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        ivHomePage = (ImageView) findViewById(R.id.iv_home_page);
        ivProperty = (ImageView) findViewById(R.id.iv_property);
        ivLife = (ImageView) findViewById(R.id.iv_life);
        ivPersonal = (ImageView) findViewById(R.id.iv_personal);

        ivHomePage.setOnClickListener(this);
        ivProperty.setOnClickListener(this);
        ivLife.setOnClickListener(this);
        ivPersonal.setOnClickListener(this);

        showFragment(HOME_PAGE);
    }

    /**
     * 显示 fragment
     *
     * @param index
     */
    private void showFragment(int index) {
        fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        initBottomBtn();
        switch (index) {
            case HOME_PAGE:
                if (homePageFragment != null) {
                    ft.show(homePageFragment);
                } else {
                    homePageFragment = new HomePageFragment();
                    ft.add(R.id.fl_container, homePageFragment, "HomePageFragment");
                }
                ivHomePage.setImageResource(R.drawable.ic_home_page_selected);
                break;
            case PROPERTY:
                if (propertyFragment != null) {
                    ft.show(propertyFragment);
                } else {
                    propertyFragment = new PropertyFragment();
                    ft.add(R.id.fl_container, propertyFragment, "PropertyFragment");
                }
                ivProperty.setImageResource(R.drawable.ic_property_selected);
                break;
            case LIFE:
                if (lifeFragment != null) {
                    ft.show(lifeFragment);
                } else {
                    lifeFragment = new LifeFragment();
                    ft.add(R.id.fl_container, lifeFragment, "LifeFragment");
                }
                ivLife.setImageResource(R.drawable.ic_life_selected);
                break;
            case PERSONAL:
                if (personalFragment != null) {
                    ft.show(personalFragment);
                } else {
                    personalFragment = new PersonalFragment();
                    ft.add(R.id.fl_container, personalFragment, "PersonalFragment");
                }
                ivPersonal.setImageResource(R.drawable.ic_personal_selected);
                break;
            default:
                break;
        }
        ft.commit();
    }

    /**
     * 初始化底部按钮状态
     */
    private void initBottomBtn() {
        ivHomePage.setImageResource(R.drawable.ic_home_page);
        ivProperty.setImageResource(R.drawable.ic_property);
        ivLife.setImageResource(R.drawable.ic_life);
        ivPersonal.setImageResource(R.drawable.ic_personal);
    }


    private void hideFragment(FragmentTransaction ft) {
        if (homePageFragment != null) {
            ft.hide(homePageFragment);
        }
        if (propertyFragment != null) {
            ft.hide(propertyFragment);
        }
        if (lifeFragment != null) {
            ft.hide(lifeFragment);
        }
        if (personalFragment != null) {
            ft.hide(personalFragment);
        }
    }

    @Override
    public void onClick(View view) {
        initBottomBtn();
        switch (view.getId()) {
            case R.id.iv_home_page:
                showFragment(HOME_PAGE);
                break;
            case R.id.iv_property:
                showFragment(PROPERTY);
                break;
            case R.id.iv_life:
                showFragment(LIFE);
                break;
            case R.id.iv_personal:
                showFragment(PERSONAL);
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (!quit) { //询问退出程序
            toast("再按一次退出程序");
            new Timer(true).schedule(new TimerTask() { //启动定时任务
                @Override
                public void run() {
                    quit = false; //重置退出标识
                }
            }, 2000);
            quit = true;
        } else { //确认退出程序
            super.onBackPressed();
            finish();
        }
    }
}
